package com.chunfeng.lerunapplication.spinner;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.chunfeng.lerunapplication.R;
import lombok.AllArgsConstructor;

import java.util.Map;
import java.util.Objects;

/**
 * 下拉框适配器
 *
 * @author by 春风能解释
 * <p>
 * 2023/12/4
 */
@AllArgsConstructor
public class SelectAdapt extends BaseAdapter {
    /**
     * 选项
     */
    private Map<Integer, Object> selectList;


    /**
     * 选项个数
     *
     * @return 个数
     */
    @Override
    public int getCount() {
        return selectList.size();
    }

    /**
     * 获取指定数据
     *
     * @param position 索引
     * @return 数据
     */
    @Override
    public Object getItem(int position) {
        return selectList.get(position);
    }

    /**
     * 获取与列表中指定位置关联的行 ID
     *
     * @param position 索引
     * @return ID
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 绑定自定义视图
     *
     * @param position    索引
     * @param convertView 转换类型
     * @param parent      索引
     * @return 指定索引的索引
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.spinner_select_layout, parent, false);
        if (Objects.nonNull(view)) {
            TextView key = view.findViewById(R.id.Status_Key);
            key.setText(String.valueOf(position));
            TextView value = view.findViewById(R.id.Status_Value);
            value.setText(selectList.get(position).toString());
        }
        return view;
    }
}
